package com.orion.controller;

import com.orion.entity.InnerTrans;
import com.orion.entity.Trans;
import com.orion.service.TestService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author Administrator
 * @date 2021/10/19
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private TestService testService;

    @GetMapping("/getWithInt")
    public Integer getWithInt(@RequestParam(name = "i") Integer i) {
        return testService.getWithInt(i);
    }

    @GetMapping("/getWithDouble")
    public Double getWithInt(@RequestParam(name = "d") Double d){
        return testService.getWithDouble(d);
    }

    @GetMapping("/getWithString")
    public String getWithString(@RequestParam(name = "str") String s) {
        return testService.getWithString(s);
    }

    @GetMapping("/getWithPvInt/{id}")
    public Integer getWithPvInt(@PathVariable(name = "id") Integer i) {
        return testService.getWithPvInt(i);
    }

    @GetMapping("/getWithPvStr/{code}")
    public String getWithPvStr(@PathVariable(name = "code") String c) {
        return testService.getWithPvStr(c);
    }

    @GetMapping("/getWithMap")
    public Map getWithMap() {
        Map<String,Integer> map = new HashMap<>(8);
        map.put("ss",12);
        map.put("ff",44);
        return testService.getWithMap(map);
    }


    @GetMapping("/getWithBean")
    public Trans getWithBean() {
        Trans trans = new Trans();
        trans.setDate(new Date());
        trans.setLdt(LocalDateTime.now().minusYears(1L));
        trans.setId(99);
        trans.setMoney(99.99);
        trans.setName("consumer-a test getWithBean");
        InnerTrans innerTrans = new InnerTrans();
        innerTrans.setAge(88);
        innerTrans.setCode("inner");
        innerTrans.setDate(new Date());
        innerTrans.setTime(LocalDateTime.now().minusYears(2L));
        trans.setInnerTrans(innerTrans);
        return testService.getWithBean(trans);
    }

    @GetMapping("/getWithSimpleBean")
    public InnerTrans getWithSimpleBean() {
        InnerTrans innerTrans = new InnerTrans();
        innerTrans.setAge(88);
        innerTrans.setCode("inner");
        innerTrans.setFavor(Arrays.asList("fefe","gggg"));
        innerTrans.setTime(LocalDateTime.now());
        innerTrans.setDate(new Date());
        return testService.getWithSimpleBean(innerTrans);
    }

    @GetMapping("/getBean")
    public Trans getBean() {
        return testService.getBean();
    }

    //----------------  post -----------------

    @PostMapping("/postWithInt")
    public Integer postWithInt(@RequestParam Integer i){
        return testService.postWithInt(i);
    }

    @PostMapping("/postWithDouble")
    public Double postWithDouble(@RequestParam Double d){
        return testService.postWithDouble(d);
    }

    @PostMapping("/postWithString")
    public String postWithString(@RequestParam String s){
        return testService.postWithString(s);
    }

    @PostMapping("/postWithMap")
    public Map postWithMap(){
        Map<String,String> map = new HashMap<>(8);
        map.put("kk","yes");
        map.put("hh","no");
        return testService.postWithMap(map);
    }

    @PostMapping("/postWithList")
    public List postWithList(){
        Trans trans = new Trans();
        trans.setDate(new Date());
        trans.setLdt(LocalDateTime.now().minusYears(1L));
        trans.setId(99);
        trans.setMoney(99.99);
        trans.setName("consumer-a test getWithBean");
        InnerTrans innerTrans = new InnerTrans();
        innerTrans.setAge(88);
        innerTrans.setCode("inner");
        trans.setInnerTrans(innerTrans);

        Trans trans2 = new Trans();
        trans2.setDate(new Date());
        trans2.setLdt(LocalDateTime.now().minusYears(1L));
        trans2.setId(88);
        trans2.setMoney(88.88);
        trans2.setName("consumer-a test getWithBean");
        InnerTrans innerTrans2 = new InnerTrans();
        innerTrans2.setAge(88);
        innerTrans2.setCode("inner");
        trans2.setInnerTrans(innerTrans2);

        List<Trans> l = new ArrayList<>();
        l.add(trans);
        l.add(trans2);
        return testService.postWithList(l);
    }

    @PostMapping("/postWithBean")
    public Trans postWithBean(){
        Trans trans = new Trans();
        trans.setDate(new Date());
        trans.setLdt(LocalDateTime.now().minusDays(2L));
        trans.setId(101);
        trans.setMoney(101.99);
        trans.setName("consumer-a test postWithBean");
        InnerTrans innerTrans = new InnerTrans();
        innerTrans.setAge(101);
        innerTrans.setCode("inner");
        trans.setInnerTrans(innerTrans);
        return testService.postWithBean(trans);
    }

    @PostMapping("/postWithSimpleBean")
    public InnerTrans postWithSimpleBean(){
        InnerTrans innerTrans = new InnerTrans();
        innerTrans.setAge(101);
        innerTrans.setCode("inner");
        innerTrans.setTime(LocalDateTime.now());
        innerTrans.setDate(new Date());
        innerTrans.setFavor(Arrays.asList("fefee","ggg"));
        return testService.postWithSimpleBean(innerTrans);
    }

    //----------------  put -----------------

    @GetMapping("/putBean")
    public Trans putBean() {
        Trans trans = new Trans();
        trans.setDate(new Date());
        trans.setLdt(LocalDateTime.now().minusYears(1L));
        trans.setId(55);
        trans.setMoney(55.55);
        trans.setName("consumer-a test putBean");
        InnerTrans innerTrans = new InnerTrans();
        innerTrans.setAge(44);
        innerTrans.setCode("inner");
        trans.setInnerTrans(innerTrans);
        return testService.putBean(trans);
    }

    //----------------  delete -----------------

    @GetMapping("/deleteInter")
    public String deleteInter() {
        return testService.deleteInter(6);
    }
}
